package com.itheima.demo.UDPdemo;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {//UDP发送数据,数据来自键盘输入,直到输入的数据为886,发送数据结束

    public static void main(String[] args) throws IOException {
        //创建成员变量Scanner,DatagramSocket,InetAddress
        Scanner sc = new Scanner(System.in);
        DatagramSocket socket = new DatagramSocket();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        String s;
        while (true) {
            System.out.println("请输入数据");
            if ((s = sc.nextLine()).equals("886")) {
                socket.send(new DatagramPacket(s.getBytes(),s.getBytes().length,address,1000));
                break;
            }else{//把每一行数据包装为一个包裹,并交码头发送
                socket.send(new DatagramPacket(s.getBytes(),s.getBytes().length,address,1000));
            }
        }
        //释放资源
        socket.close();
    }
}
